<?php

class UsersController extends Zend_Controller_Action
{

    public function init()
    {
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }

    public function indexAction()
    {
        $this->view->title = 'Список пользователей';
        
        $roles = new Application_Model_Roles();
        $role = $roles->getRole(Zend_Auth::getInstance()->getIdentity()->user_role_id);
        $this->view->role = $role;
        
        $users = new Application_Model_Users();
        $this->view->users = $users->getAllUsers();
        
    }

    public function registrationAction()
    {
        $this->view->title = 'Жаңы колдонуучуну катто';
        $form = new Application_Form_Registration();
        if ($this->_request->isPost())
        {   
            if ($form->isValid($this->_request->getPost()))
            {
                $data = $this->_request->getPost();
                $user = new Application_Model_Users();
                $user->fill($data);
                $user->user_password = sha1($data['user_password']);
                $user->user_birthday = time();
                $user->user_creation_date = time();
                $user->user_creation_ip   = $_SERVER['REMOTE_ADDR'];
                $user->user_language_id   = 1;
                $user->user_lastlogin_ip  = $_SERVER['REMOTE_ADDR'];
                $user->user_lastlogin_date = time();  
                $user->user_status        = 'Привет Всем!!!';
                $user->user_activ_code    = '1';
                $user->user_role_id       = 2;
                $user->save();
                $this->_redirect('users/login');
            }
        }
        $this->view->form = $form;
    }

    public function loginAction()
    {
        $this->view->title = 'Авторизация';
        $form = new Application_Form_Auth();
        $user = new Application_Model_Users();
        if ($this->_request->isPost())
        {
            if ($form->isValid($this->_request->getPost()))
            {
                if ($user->authorize($form->getValue('login'), $form->getValue('password')))
                {
                }
                else 
                {
                    echo "Да пошел ты!!!";
                }
            }
        }
        if (Zend_Auth::getInstance()->hasIdentity())
        {
            $roles = new Application_Model_Roles();
            $role = $roles->getRole(Zend_Auth::getInstance()->getIdentity()->user_role_id);
            $this->view->role = $role;
        }
        else {
            $this->_helper->layout->disableLayout();
        }
        $this->view->form = $form;
    }

    public function logoutAction()
    {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_redirect('users/login');
    }

    public function editAction()
    {
        $this->view->title = 'Редактирование пользователя';
        $id = $this->_request->getParam('id');
        $user = new Application_Model_Users($id);
        $form = new Application_Form_Registration();
        
        if ($this->_request->isPost()){   
            if ($form->isValid($this->_request->getPost())){               
                $user->fill($this->_request->getPost());
                $user->save();
                $this->_redirect('users');
            } 
        } else {
                $form->populate($user->populateForm());
        }
        $this->view->form = $form;
    }

    public function deleteAction()
    {
        $this->view->title = 'Удаление пользователя';
        $id = $this->_getParam('id');
        $user = new Application_Model_Users($id);
        $user->delete();
        $this->_redirect('users');
    }

    public function viewAction()
    {
        $this->view->title = 'Профильди көрүү';
        $id = $this->_getParam('id');
        $user = new Application_Model_Users($id);
        $this->view->user = $user;
        
    }
}





